<?php
//
namespace Admin\Controller;
use Think\Controller;
class StudentController extends Controller {
        //展示专业和班级
        public function studentInfo (){
                $model = M('student');
                // 使用I方法接受参数class_id 如果undefined 则使用默认值1
                $class_id = I('param.class_id', 1); // 变量名 默认值 过滤方法
                $where = array('class_id' => $class_id);
                $student_info = $model -> where($where) -> select();
                $this -> assign('class_id', $class_id);
                $this -> assign('student_info', $student_info);
                //D()方法实例化模型，然后通过关联模型获取专业以及班级数据
                $major_info = D('major') -> relation(true) -> select();//方法
                //使用assign()方法分配数据
                $this -> assign("major_info", $major_info);
                $this -> display();
	}

        public function addStudent() {
                $model = M('student');
                $class_id = I('get.class_id');
                if(IS_POST) {
                        $model -> create();
                        if($model -> add()) {
                                $this -> success('成功', U("studentInfo?class_id={$class_id}"));
                                return;
                        }
                }
                $major_info = D('major') -> relation(true) -> select();  //方法
                $this -> assign("major_info", $major_info);
                $this -> assign("class_id", $class_id);
                $this -> display();
        }

        public function update() {
                $model = M('student');
                $where = array('student_id' => I('get.student_id'));
                if(IS_POST) {
                        $student_info = $model -> create();
                        if($model -> save() !== false) {
                                $this -> success('成功', U("studentInfo?class_id={$student_info['class_id']}"));
                                return;
                        }
                        $this -> error('失败');
                }
                $student_info = $model -> where($where) -> find();
                if(!isset($student_info)) {
                        $this -> error('查询不到');
                        return;
                }
                $major_info = D('major') -> relation(true) -> select();
                $this -> assign("major_info", $major_info);
                $this -> assign("student_info", $student_info);
                $this -> display();
        }
        public function delete() {
                $model = M('student');
                $where = array('student_id' => I('get.student_id'));
                $class_id = I('student_id');
                $res = $model -> where($where) -> delete();
                if($res === false) {
                        $this -> error('删除失败');
                        return;
                }else if($res === 0) {
                        $this -> error('删除不存在');
                        return;
                }
                $this -> success('删除成功', U("studentInfo?class_id={'$class_id'}"));
        }
}